package com.quectel.cms.controller.really;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.really.dto.ReallyEquipmentDto;
import com.quectel.core.module.really.service.ReallyEquipmentService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-09 14:25:55
 */
@RestController
@RequestMapping("reallyEquipment")
@Tag(name = "ReallyEquipmentController", description = "实有装备相关api")
public class ReallyEquipmentController extends BaseController {

    @DubboReference
    private ReallyEquipmentService reallyEquipmentService;

    /**
     * 实有装备分页列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "reallyEquipment:list")
    @Operation(summary = "实有装备分页列表")
    public Response<Page<ReallyEquipmentDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = reallyEquipmentService.queryTotal(request.getParams());

        List<ReallyEquipmentDto> list = null;
        if (total > 0) {
            list = reallyEquipmentService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<ReallyEquipmentDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<ReallyEquipmentDto>>ok().wrap(page);
    }


    /**
     * 查询实有装备单条记录
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "reallyEquipment:info")
    @Operation(summary = "查询实有装备单条记录")
    public Response<ReallyEquipmentDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (reallyEquipmentService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        ReallyEquipmentDto reallyEquipmentDto = reallyEquipmentService.selectById(id);

        return Response.<ReallyEquipmentDto>ok().wrap(reallyEquipmentDto);
    }

    /**
     * 保存实有装备单条记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "reallyEquipment:edit")
    @Operation(summary = "保存实有装备单条记录")
    public Response<Object> save(@RequestBody @Validated ReallyEquipmentDto reallyEquipmentDto) {

        //填充基础数据
        fillSaveCommonData(reallyEquipmentDto);

        reallyEquipmentService.save(reallyEquipmentDto);

        return Response.ok();
    }

    /**
     * 修改实有装备单条记录
     */
    @PostMapping("update")
    @RequirePermissions(values = "reallyEquipment:edit")
    @Operation(summary = "修改实有装备单条记录")
    public Response<Object> update(@RequestBody @Validated ReallyEquipmentDto reallyEquipmentDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(reallyEquipmentDto.getId()));
        if (reallyEquipmentService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(reallyEquipmentDto);

        reallyEquipmentService.updateById(reallyEquipmentDto);

        return Response.ok();
    }

    /**
     * 批量删除实有装备记录
     */
    @PostMapping("delete")
    @RequirePermissions(values = "reallyEquipment:delete")
    @Operation(summary = "批量删除实有装备记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (reallyEquipmentService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        reallyEquipmentService.deleteBatch(ids);

        return Response.ok();
    }
}
